package com.six.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.six.dao.ImgsDaoImpI;
import com.six.dao.ProductDaoImpl;
import com.six.dao.ProducttypeDaoImpl;

import com.six.entity.Imgs;
import com.six.entity.Product;
import com.six.entity.producttype;

/**
 * 地址：localhost:8080/Dii-six-item/ProductServlet?method=toDisplay
 * 
 * @author DELL
 *
 */
@WebServlet("/ProductServlet")
public class ProductServlet extends BaseServlet {
	/**
	 * 产品的类型
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void toDisplay(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		ProductDaoImpl prodao = new ProductDaoImpl();
		ArrayList<Product> prolist = prodao.getAllList();
		
		ProducttypeDaoImpl dao = new ProducttypeDaoImpl();
		ArrayList<producttype> typee = dao.getAllProType();
		
		request.setAttribute("prolist", prolist);
		request.setAttribute("protype", typee);
		
		request.getRequestDispatcher("productlist.jsp").forward(request, response);

	}

	/*	  
		  *//**
				 * 产品的类型
				 * 
				 * @param req
				 * @param resp
				 * @throws ServletException
				 * @throws IOException
				 *//*
					 * public void getProByType(HttpServletRequest req, HttpServletResponse resp)
					 * throws ServletException, IOException { //通过name获取值 String typeId =
					 * req.getParameter("typeId");
					 * 
					 * // 产品类型列表 ProducttypeDaoImpl Protype = new ProducttypeDaoImpl();
					 * ArrayList<producttype> allProtype = Protype.getAllProType();
					 * 
					 * //产品列表 ProductDaoImpl proDao = new ProductDaoImpl(); ArrayList<Product>
					 * proList = proDao.getAllList();
					 * 
					 * HttpSession session = req.getSession(); session.setAttribute("allProtype",
					 * allProtype); req.setAttribute("proList", proList);
					 * 
					 * // 跳转页面 req.getRequestDispatcher("productlist.jsp").forward(req, resp); }
					 */

	/**
	 * 产品详情
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void doProductDetails(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		// 通过name获取值
		String id = req.getParameter("id");

		// 产品类型列表
		ProducttypeDaoImpl dao = new ProducttypeDaoImpl();
		ArrayList<producttype> typee = dao.getAllProType();
	
		int ids = 0;
		if (id != null && !id.equals("")) {
			ids = Integer.valueOf(id);
		}
		// 图片列表
		ImgsDaoImpI imgDao = new ImgsDaoImpI();
		ArrayList<Imgs> imglist = imgDao.getImagesList(ids);

		// 产品列表
		ProductDaoImpl proDao = new ProductDaoImpl();
		Product pro = proDao.getProById(ids);

		req.setAttribute("protype", typee);
		req.setAttribute("pro", pro);
		req.setAttribute("imglist", imglist);

		// 跳转页面
		req.getRequestDispatcher("productdetails.jsp").forward(req, resp);
	}

}
